package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarCondition;
import com.feicheng.lanniao.entity.CarRentalPrice;
import com.feicheng.lanniao.entity.CarSellClientVo;
import com.feicheng.lanniao.entity.CarSellPrice;
import com.feicheng.lanniao.entity.Mileage;
import com.feicheng.lanniao.mapper.CarSellClientVoMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarConditionService;
import com.feicheng.lanniao.service.ICarRentalPriceService;
import com.feicheng.lanniao.service.ICarSellClientVoService;
import com.feicheng.lanniao.service.ICarSellPriceService;
import com.feicheng.lanniao.service.IMileageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-07
 */
@Service
public class CarSellClientVoServiceImpl extends ServiceImpl<CarSellClientVoMapper, CarSellClientVo> implements ICarSellClientVoService {
	@Autowired
	private ICarConditionService iCarConditionService;
	@Autowired
	private IMileageService IMileageService;
	@Autowired
	private ICarSellPriceService iCarSellPriceService;
	/**
	 * @see.ICarSellClientVoService.getCarSellClientVoPage
	 */
	@Override
	public ResultInfo getCarSellClientVoPage(String searchKey,Boolean dec,String order,Integer clientId , Integer conditionId, Integer mileageId, Integer brandId,
			Integer sellPriceId, Integer carSellPriceFrom, Integer carSellPriceTo,String type, String ownerShip,Integer currentPage, Integer pageSize) {

		QueryWrapper<CarSellClientVo> carQueryWapper = new QueryWrapper<CarSellClientVo>();
		//判断手动价格搜索条件是否为空
		if(null !=carSellPriceFrom && null != carSellPriceTo) {
			if(carSellPriceFrom == 0 && carSellPriceTo !=0) {
				carQueryWapper.lt("sell_price", carSellPriceTo);
			}
			else if(carSellPriceFrom != 0 && carSellPriceTo ==0) {
				carQueryWapper.gt("sell_price", carSellPriceFrom);
			}
			else if (carSellPriceFrom != 0 && carSellPriceTo !=0) {
				carQueryWapper.between("sell_price", carSellPriceFrom, carSellPriceTo);
			}
		}
		//判断排序条件order是否为空
		if(StringUtils.hasLength(order)) {
			if (!order.equals("default")) {
				if(dec) {
					carQueryWapper.orderByDesc(order);
				}
				else {
					carQueryWapper.orderByAsc(order);
				}
			}
			else {
				carQueryWapper.orderByAsc("name");
			}
		}

		//判断查询条件searchKey是否为空，不为空则与数据名字关联
		if(StringUtils.hasLength(searchKey)) {
			carQueryWapper.and(i -> i.like("type", searchKey)
			.or().like("owner_ship", searchKey)
			.or().like("car_condition", searchKey)
			.or().like("mileage", searchKey));
		}

		//判断查询条件conditionId是否为空，不为空则与数据名字关联
		if (null !=conditionId && conditionId!=0) {
			CarCondition carCondition = iCarConditionService.getById(conditionId);
			carQueryWapper.between("car_condition",carCondition.getMinCondition(), carCondition.getMaxCondition());
		}

		//判断查询条件mileageId是否为空或0，不为空或0则与数据名字关联
		if (null !=mileageId && mileageId!=0) {

			Mileage mileage = IMileageService.getById(mileageId);
			carQueryWapper.between("mileage", mileage.getMinMileage(), mileage.getMaxMilleage());
		}

		//判断查询条件brandId是否为空或0，不为空或0则与数据名字关联
		if(null !=brandId && brandId!=0) {
			carQueryWapper.eq("brand_id", brandId);
		}

		//判断查询条件clientId是否为空或0，不为空或0则与数据名字关联
		if(null !=clientId && clientId!=0) {
			carQueryWapper.eq("client_id", clientId);
		}

		//判断查询条件卖车价格是否为空或0，不为空或0则与数据名字关联
		if(null !=sellPriceId && sellPriceId!=0) {
			CarSellPrice carSellPrice = iCarSellPriceService.getById(sellPriceId);
			carQueryWapper.between("sell_price", carSellPrice.getMinPrice(), carSellPrice.getMaxPrice());
		}
		//判断查询条件车辆类型是否为空，不为空则与数据名字关联
		if(StringUtils.hasLength(type)) {			
			carQueryWapper.like("type", type);
		}
		//判断查询条件车辆是否为空，不为空则与数据名字关联
		if(StringUtils.hasLength(ownerShip)) {			
			carQueryWapper.like("owner_ship", ownerShip);
		}
		IPage<CarSellClientVo> pageInfo = new Page<CarSellClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, carQueryWapper));

	}

	/**
	 * @see.ICarSellClientVoService.getCarSellClientVoById
	 */
	@Override
	public ResultInfo getCarSellClientVoById(Integer carSellInfoId) {
		QueryWrapper<CarSellClientVo> carQueryWapper = new QueryWrapper<CarSellClientVo>();
		//判断查询条件carSellInfoId是否为空或0，不为空或0则与数据名字关联
		if(null !=carSellInfoId && carSellInfoId!=0) {
			carQueryWapper.eq("car_sell_info_id", carSellInfoId);
		}
		return ResultInfo.success(this.getOne(carQueryWapper));
	}

	/**
	 * @see.ICarSellClientVoService.getCarSellClientVoPage
	 */
	@Override 
	public ResultInfo getCarSellClientVoPage(String searchKey, Integer clientId, Integer conditionId, Integer mileageId,
			Integer brandId, Integer sellPriceId,String typeName, Integer currentPage, Integer pageSize) {
		// TODO Auto-generated method stub
		QueryWrapper<CarSellClientVo> carQueryWapper = new QueryWrapper<CarSellClientVo>();

		//判断查询条件searchKey是否为空，不为空则与数据名字关联
		if(StringUtils.hasLength(searchKey)) {
			carQueryWapper.and(i -> i.like("name", searchKey));
//			.or().like("color", searchKey)
//			.or().like("owner_ship", searchKey)
//			.or().like("transmission_case", searchKey)
//			.or().like("type", searchKey)
//			.or().like("energy", searchKey)
//			.or().like("drive", searchKey));

		}


		if(StringUtils.hasLength(typeName)){
			
			carQueryWapper.eq("type", typeName);
		}

		//判断查询条件conditionId是否为空，不为空则与数据名字关联
		if (null !=conditionId && conditionId!=0) {
			CarCondition carCondition = iCarConditionService.getById(conditionId);
			carQueryWapper.between("car_condition",carCondition.getMinCondition(), carCondition.getMaxCondition());
		}

		//判断查询条件mileageId是否为空或0，不为空或0则与数据名字关联
		if (null !=mileageId && mileageId!=0) {

			Mileage mileage = IMileageService.getById(mileageId);
			carQueryWapper.between("mileage", mileage.getMinMileage(), mileage.getMaxMilleage());
		}

		//判断查询条件brandId是否为空或0，不为空或0则与数据名字关联
		if(null !=brandId && brandId!=0) {
			carQueryWapper.eq("brand_id", brandId);
		}

		//判断查询条件clientId是否为空或0，不为空或0则与数据名字关联
		if(null !=clientId && clientId!=0) {
			carQueryWapper.eq("client_id", clientId);
		}

		//判断查询条件卖车价格是否为空或0，不为空或0则与数据名字关联
		if(null !=sellPriceId && sellPriceId!=0) {
			CarSellPrice carSellPrice = iCarSellPriceService.getById(sellPriceId);
			carQueryWapper.between("sell_price", carSellPrice.getMinPrice(), carSellPrice.getMaxPrice());
		}

		IPage<CarSellClientVo> pageInfo = new Page<CarSellClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, carQueryWapper));
	}

}
